package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.RightService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @version V1.0
 * @author: hanshitong
 * @date: 2019-08-08 9:55
 * @description:
 */
@RestController
@RequestMapping("/right")
public class RightController {
    //kk
    @Reference
    private RightService rightService;

    /**
     *
     * @param queryPageBean
     * @return 分页查询
     */
    @RequestMapping("/rightAll")
    public PageResult rightAll(@RequestBody QueryPageBean queryPageBean)
    {
        return rightService.rightAll(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
    }

    /**
     * 权限添加
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission)
    {
        try {
            rightService.add(permission);
            return new Result(true, MessageConstant.ADD_RIGHT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_RIGHT_FAIL);
        }
    }


    /**
     *  编辑查询单条
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id)
    {
        try {
            Permission data =  rightService.findById(id);
            return new Result(true,MessageConstant.EDIT_RIGHT_SUCCESS,data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_RIGHT_FAIL);
        }
    }


    /**
     *  编辑修改
     * @param permission
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission)
    {
        try {
            rightService.edit(permission);
            return new Result(true,MessageConstant.EDIT_RIGHT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_RIGHT_FAIL);
        }
    }

    /**
     *  权限删除
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id)
    {
        try {
            rightService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
    /**
     *  c查询都有权限
     * @param
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll()
    {
        try {
            List<Permission> listPermissions =  rightService.findAll();
            return new Result(true,MessageConstant.EDIT_RIGHT_SUCCESS,listPermissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_RIGHT_FAIL);
        }
    }
}